package com.jn.erp.web.api.interaction.controller;


import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.common.entity.TLabel;
import com.jn.erp.web.api.common.service.TLabelService;
import com.jn.erp.web.api.interaction.entity.TEvaluateTemplate;
import com.jn.erp.web.api.interaction.entity.dto.TEvaluateTemplateDto;
import com.jn.erp.web.api.interaction.service.TEvaluateTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyp
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/tEvaluateTemplate")
public class TEvaluateTemplateController extends BaseWebAuthController {
    @Autowired
    private TLabelService tLabelService;

    @Autowired
    private TEvaluateTemplateService tEvaluateTemplateService;

    /**
     * 评价模版新增新建
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody TEvaluateTemplateDto tEvaluateTemplateDto) {

        //机构id
        tEvaluateTemplateDto.setOrgId(Long.parseLong(loginDeptAuth()));
        tEvaluateTemplateDto.setCreateUser(getUserId().toString());
        Boolean boo = tEvaluateTemplateService.saveTemplate(tEvaluateTemplateDto);
        return AjaxResult.success(boo);
    }


    /**
     * 评价模版修改
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody TEvaluateTemplateDto tEvaluateTemplateDto) {

        //当前登录人id
        tEvaluateTemplateDto.setUpdateUser(String.valueOf(getUserId()));
        //机构id
        tEvaluateTemplateDto.setOrgId(Long.parseLong(loginDeptAuth()));
        tEvaluateTemplateDto.setOrgId(Long.parseLong(loginDeptAuth()));
        tEvaluateTemplateDto.setCreateUser(getUserId().toString());
        Boolean boo = tEvaluateTemplateService.updateTemplate(tEvaluateTemplateDto);
        return AjaxResult.success(boo);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    public TableDataInfo templateList(TEvaluateTemplate tEvaluateTemplate) {
        tEvaluateTemplate.setOrgId(Long.parseLong(loginDeptAuth()));
        List<TEvaluateTemplateDto> list = tEvaluateTemplateService.templateList(tEvaluateTemplate);
        return getDataTable(list);
    }


    /**
     * 评价模版删除
     */
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody TEvaluateTemplate tEvaluateTemplate) {
        //机构id
        tEvaluateTemplate.setOrgId(Long.parseLong(loginDeptAuth()));
        tEvaluateTemplate.setIsDelete("1");
        Long id = tEvaluateTemplate.getId();
       List<TLabel> label = tLabelService.selById(id);
        Boolean boo = tEvaluateTemplateService.updateById(tEvaluateTemplate);
        return AjaxResult.success(boo);
    }

    /**
     * 模板关联课程
     *
     * @param tEvaluateTemplate
     * @return
     */
    @PostMapping("courseTemplateList")
    public AjaxResult courseTemplateList(@RequestBody TEvaluateTemplate tEvaluateTemplate) {
        //机构id
        tEvaluateTemplate.setOrgId(Long.parseLong(loginDeptAuth()));
        List<TEvaluateTemplate> list = tEvaluateTemplateService.courseTemplateList(tEvaluateTemplate);
        return AjaxResult.success(list);
    }


    // 模版停用启用
    @PostMapping("templateStatus")
    public AjaxResult templateStatus(@RequestBody TEvaluateTemplate tEvaluateTemplate) {
        //机构id
        tEvaluateTemplate.setOrgId(Long.parseLong(loginDeptAuth()));
        tEvaluateTemplate.setState(tEvaluateTemplate.getState());
        boolean b = tEvaluateTemplateService.updateById(tEvaluateTemplate);
        return AjaxResult.success(b);
    }

    @PostMapping("getTempById")
    public AjaxResult getTempById(@RequestBody TEvaluateTemplate tEvaluateTemplate) {
        tEvaluateTemplate.setOrgId(Long.parseLong(loginDeptAuth()));
        List<TEvaluateTemplateDto> list = tEvaluateTemplateService.templateList(tEvaluateTemplate);
        List<TLabel> tLabels1 = new ArrayList<>();
        for (TEvaluateTemplateDto tEvaluateTemplateDto : list) {
            // 标签名称
            String labelName1 = tEvaluateTemplateDto.getLabelName();
            String[] splitName = labelName1.split(",");
            // 标签id
            String labelId = tEvaluateTemplateDto.getLabelId();
            String[] splitId = labelId.split(",");
            // 标签排序
            String[] splitNum = tEvaluateTemplateDto.getOrderNum().split(",");

//            if (splitName.length == splitNum.length
//                    && splitName.length == splitId.length) {
                int len = splitNum.length;
                for (int i = 0; i < len; i++) {
                    TLabel tLabel = new TLabel();
                    tLabel.setOrderNum(Integer.valueOf(splitNum[i]));
                    tLabel.setId(Long.valueOf(splitId[i]));
                    tLabel.setName(splitName[i]);
                    tLabels1.add(tLabel);
                }
                tEvaluateTemplateDto.settName(tLabels1);
//            } else {
//                throw new RuntimeException("数据出错请检查数据！！！");
//            }
        }

        return AjaxResult.success(list);
    }
}

